package anc.tankshooter.network.ui;

import org.andengine.entity.sprite.Sprite;
import org.andengine.entity.sprite.TiledSprite;
import org.andengine.entity.text.Text;
import org.andengine.input.touch.TouchEvent;

import anc.tankshooter.GameActivity;
import anc.tankshooter.models.IDymanicObject.MovingDirection;
import anc.tankshooter.models.ITank;
import anc.tankshooter.ui.IScreen;
import anc.tankshooter.ui.ResourcesManager;
import anc.tankshooter.ui.SceneManager.SceneType;

public class MatchResultScreen extends IScreen{
	
	private Sprite mBackground ;
	private TiledSprite mTitleDialog;
	private Text mWinnerText,mLoserText;
	
	private TiledSprite mBtnClose;
	private ResultDialogWatcher mWatcher;
	
	
	public MatchResultScreen(String winner,String loser,ITank winTank,ITank loseTank,ResultDialogWatcher watcher){
		mWatcher=watcher;
		float x = GameActivity.CAMERA_WITDH/2 - 390/2;
		float y = GameActivity.CAMERA_HEIGHT/2 - 300/2;
		attachChild(mBackground);
		attachChild(mTitleDialog); mTitleDialog.setCurrentTileIndex(6);
		winTank.setScale(2f); loseTank.setScale(2f);
		winTank.setDir(MovingDirection.RIGHT); loseTank.setDir(MovingDirection.LEFT);
		winTank.setX(x+50); loseTank.setX(x+300);
		winTank.setY(y+100);loseTank.setY(y+100);
		attachChild(winTank); attachChild(loseTank);
		attachChild(mWinnerText); mWinnerText.setText(winner);
		attachChild(mLoserText); mLoserText.setText(loser);
		attachChild(mBtnClose); registerTouchArea(mBtnClose);
		
		this.setBackgroundEnabled(false);
	}
	
	@Override
	protected void createScene() {
		float x = GameActivity.CAMERA_WITDH/2 - 390/2;
		float y = GameActivity.CAMERA_HEIGHT/2 - 300/2;
		
		mBackground = new Sprite(x,y,ResourcesManager.getInstance().mPanelTexture,  ResourcesManager.getInstance().mVertexManager);
		mTitleDialog=new TiledSprite(x,y+15, ResourcesManager.getInstance().mDialogTitlesTexture, ResourcesManager.getInstance().mVertexManager);
		
		mWinnerText = new Text(x+50, y+150, ResourcesManager.getInstance().mWhiteFont20,"0",10, ResourcesManager.getInstance().mVertexManager);
		mLoserText = new Text(x+300, y+150, ResourcesManager.getInstance().mWhiteFont20,"0",10, ResourcesManager.getInstance().mVertexManager);
		mBtnClose = new TiledSprite(x+390/2 - ResourcesManager.getInstance().mBtnTexture.getWidth()/2, y+200,ResourcesManager.getInstance().mBtnTexture, mVertextManager){
			@Override
			public boolean onAreaTouched(TouchEvent pSceneTouchEvent,
					float pTouchAreaLocalX, float pTouchAreaLocalY) {
				if(pSceneTouchEvent.getAction()==TouchEvent.ACTION_UP||pSceneTouchEvent.getAction()==TouchEvent.ACTION_OUTSIDE||
						pSceneTouchEvent.getAction()==TouchEvent.ACTION_CANCEL){
					mBtnClose.setCurrentTileIndex(6);
					if(pSceneTouchEvent.getAction()==TouchEvent.ACTION_UP) {
						mWatcher.onClose();
					}
				}else if(pSceneTouchEvent.getAction()==TouchEvent.ACTION_DOWN) {
					mBtnClose.setCurrentTileIndex(7);
				}
				return super.onAreaTouched(pSceneTouchEvent, pTouchAreaLocalX, pTouchAreaLocalY);
			}
		};
		mBtnClose.setCurrentTileIndex(6);
	}

	@Override
	public void onUpdateScreen(float second) {
	}

	@Override
	public boolean onBackKeyPressed() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public SceneType getSceneType() {
		return null;
	}

	@Override
	public void disposeScene() {
		
	}
		
	public static interface ResultDialogWatcher{
		public void onClose();
	}
}
